local CreatObjForm =BaseClass(Singleton)
local path=AssetUtils.GetPrefabPath("CreatObj_Itme")
local n=0
CreatObjForm.TempTab={
    [1] = { type = 1, l_nameId = 180000, l_descId = 180100 },
    [2] = { type = 2, l_nameId = 180001, l_descId = 180101 },
    [3] = { type = 3, l_nameId = 180002, l_descId = 180102 },
    [4] = { type = 4, l_nameId = 180003, l_descId = 180103 },
    [5] = { type = 5, l_nameId = 180004, l_descId = 180104 },
    [6] = { type = 6, l_nameId = 180005, l_descId = 180105 },
    [7] = { type = 7, l_nameId = 180006, l_descId = 180106 },
    [8] = { type = 8, l_nameId = 180007, l_descId = 180107 },
    [9] = { type = 9, l_nameId = 180008, l_descId = 180108 },
    -- [10] = { type = 10, l_nameId = 180009, l_descId = 180109 },
    -- [11] = { type = 11, l_nameId = 180010, l_descId = 180110 },
    -- [12] = { type = 12, l_nameId = 180011, l_descId = 180111 },
    -- [13] = { type = 13, l_nameId = 180012, l_descId = 180112 },
    -- [14] = { type = 14, l_nameId = 180013, l_descId = 180113 },
    -- [15] = { type = 15, l_nameId = 180014, l_descId = 180114 },
    -- [16] = { type = 16, l_nameId = 180015, l_descId = 180115 },
    -- [17] = { type = 17, l_nameId = 180016, l_descId = 180116 },
    -- [18] = { type = 18, l_nameId = 180017, l_descId = 180117 },
    -- [19] = { type = 19, l_nameId = 180018, l_descId = 180118 },
    -- [20] = { type = 20, l_nameId = 180019, l_descId = 180119 },
    -- [21] = { type = 21, l_nameId = 180020, l_descId = 180120 },
    -- [22] = { type = 28, l_nameId = 180021, l_descId = 180121 },
    -- [23] = { type = 29, l_nameId = 180022, l_descId = 180122 },
    -- [24] = { type = 30, l_nameId = 180023, l_descId = 180123 },
    -- [25] = { type = 31, l_nameId = 180024, l_descId = 180124 },
    -- [26] = { type = 51, l_nameId = 180025, l_descId = 180125 },
    -- [27] = { type = 52, l_nameId = 180026, l_descId = 180126 },
    -- [28] = { type = 53, l_nameId = 180027, l_descId = 180127 },
    -- [29] = { type = 54, l_nameId = 180028, l_descId = 180128 },
    -- [30] = { type = 55, l_nameId = 180029, l_descId = 180129 }
}
CreatObjForm.TempImg={

}
function CreatObjForm:__init()
    local Ui_Button=GameObject.Find("Canvas/Panel/LookGame")
    if Ui_Button == nil then
        print("Button is nil ")
    else
        CS.UtilsCSharp.AddButtonOnClick(Ui_Button,function(go)
            print(go.name)
            if n==0 then               
            CreatObjForm.CreatObj()
            n=n+1 
            end
            CreatObjForm:OpenUI("CreatObj")
        end)
    end
    local Ui_Close=GameObject.Find("Canvas/Panel/CreatObj/Close")
    if Ui_Close == nil then
        print("Button is nil ")
    else
        CS.UtilsCSharp.AddButtonOnClick(Ui_Close,function(Close)
            print(Close.name)
            CreatObjForm:CloseUI("CreatObj")
            
       end )
    end
    

    
end
function CreatObjForm:OpenUI(name)
    local COB=  GameObject.Find("Canvas/Panel/CreatObj")
    if COB == nil then
        print("COB is nil")
    end
        CS.UtilsCSharp.AddOnDrag(COB,function(eventData) 
        COB.transform.position = Vector3(eventData.position.x,eventData.position.y,0)
    end)
    local Name_G=GameObject.Find("Canvas/Panel/".. tostring(name))
    if Name_G == nil then 
        print("Name_G is nil")
    else
        Name_G.gameObject:SetActive(true)
    end
    print("Lua Opening UI: " .. name)
end
function CreatObjForm.CreatObj()
    local Obj_Parent=GameObject.Find("Canvas/Panel/CreatObj/Scroll View/Viewport/Content").transform
    local AssetLoadFinish=function(obj)
    for i=1,#CreatObjForm.TempTab do
            local gameObj=GameObject.Instantiate(obj)
            gameObj.transform:SetParent(Obj_Parent )
                ---设置图片
                local Temp_Icon=AssetUtils.GetImgPath(CreatObjForm.TempTab[i].l_nameId)
                local load = function(temp_Img)
                -- 确保能找到子物体 "Icon"
                local img = AssetUtils.FindChildCom_One(gameObj, "Icon")
                CreatObjForm.TempImg[i]=img
                if img then
                    -- 确保组件类型是 UnityEngine.UI.Image
                    local imageComp = img:GetComponent(typeof(CS.UnityEngine.UI.Image))
                    if imageComp then
                        imageComp.sprite = temp_Img
                    else
                        print("Image component not found on Icon.")
                    end
                else
                    print("Icon not found in gameObj")
                end
                end
                ResMgr:GetInstance():LoadSpriteAsset(Temp_Icon, load)

            end
        end
        ResMgr:GetInstance():LoadPrefabAsset(path,AssetLoadFinish)
        CreatObjForm.OnButtonClick()
end
function CreatObjForm:CloseUI(name)
     local Name_G=GameObject.Find("Canvas/Panel/".. tostring(name))
    if Name_G == nil then 
        print("Name_G is nil")
    else
        Name_G.gameObject:SetActive(false)
    end
    print("Lua Closing UI: " .. name)
end


function CreatObjForm.OnButtonClick()
    for i=1,#CreatObjForm.TempImg do
        local but=CreatObjForm.TempImg[i].gameObject
         CS.UtilsCSharp.AddButtonOnClick(but,function(go)
         print("点击")   
         Entity.CreatEntity(CreatObjForm.TempImg[i],CreatObjForm.TempTab[i])
        end)
        
    end
end
return CreatObjForm